from time import sleep
# 导入webdriver包
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


class Sina(object):
    def __init__(self):
        print("刘世琪作业")
        self.url = "https://news.sina.com.cn/"
        self.driver = webdriver.Firefox()
        # 窗口自定义尺寸
        self.driver.set_window_size(900, 500)
        self.action = ActionChains(self.driver)
        # 隐式等待
        self.driver.implicitly_wait(1)

        # self.user = input("请输入账号")
        # self.pwd = input("请输入密码")
        # self.pwds = input("请输入确定密码")

    # 登录
    # def get_login(self):
    #     self.driver.get(self.url)
    #     print(self.driver.window_handles)
    #     self.driver.find_element_by_xpath('//*[@id="mobile"]').send_keys(self.user)
    #     self.driver.find_element_by_xpath('//*[@id="password"]').send_keys(self.pwd)
    #     self.driver.find_element_by_xpath('//*[@id="passwordConfirm"]').send_keys(self.pwds)
    #     sleep(4)

    # 新浪网首页
    def get_news(self):
        self.driver.get(self.url)
        print("刘世琪作业")
        # print(self.driver.window_handles)
        # # 跳转到新的窗口
        # 滚动到底部
        js = "var q=document.documentElement.scrollTop=10000"  # documentElement表示获取根节点元素
        self.driver.execute_script(js)
        sleep(5)
        # 滚动到顶部
        script = "window.scrollTo(0,0)"
        print("刘世琪作业")
        self.driver.execute_script(script)
        sleep(2)
        print("刘世琪作业")
        script = "window.scrollTo(1000000,1000)"
        self.driver.execute_script(script)
        self.driver.find_element_by_xpath("//*[@id='blk_cNav2_01']/div/a[15]/span").click()
        # self.driver.switch_to.window(self.driver.window_handles[1])
        # self.driver.find_element_by_partial_link_text("科技").click()
        # sleep(5)

    # 新浪网科技 鼠标移动到数码菜单上，然后选择电脑
    def Numerical(self):
        # print(self.driver.window_handles)
        # 定位数码用的鼠标悬浮
        more = self.driver.find_element_by_partial_link_text("数码")
        print("刘世琪作业")
        # more = self.driver.find_element_by_xpath("//*[@id='tech_head']/div[7]/div/a")
        self.action.move_to_element(more).perform()
        print("刘世琪作业")
        sleep(1)
        # 查询电脑  点击他
        self.driver.find_element_by_partial_link_text("电脑").click()
        print("刘世琪作业")
        # self.driver.find_element_by_xpath("//*[@id='tech_head']/div[7]/div/ul/li[1]/a").click()

    def Wicket(self):
        print("获取当前窗口句柄：", self.driver.window_handles)
        print("刘世琪作业")
        print("获取当前所有窗口句柄：", self.driver.current_window_handle)
        print("刘世琪作业")
        # 获取当前窗口句柄（窗口A）
        handle = self.driver.current_window_handle
        # 获取当前所有窗口句柄（窗口A、B）
        handles = self.driver.window_handles
        # 对窗口进行遍历
        for newhandle in handles:
            # 筛选新打开的窗口B
            if newhandle != handle:
                # 切换到新打开的窗口B
                self.driver.switch_to.window(newhandle)
                # 关闭当前窗口A
                sleep(1)
                self.driver.switch_to.window(handles[0])
                self.driver.close()
                sleep(1)

    def run(self):
        # 新浪网首页
        self.get_news()
        # 新浪网科技 鼠标移动到数码菜单上，然后选择电脑
        self.Numerical()
        # 关闭当前窗口，在关闭第二个窗口
        self.Wicket()
        # self.get_login()

    def Quit(self):
        self.driver.quit()
        self.driver.quit()


if __name__ == '__main__':
    liushiqi = Sina()
    liushiqi.run()
    liushiqi.Quit()
